package com.sh.learn.itcast.javaadvanced.aop;

import java.io.InputStream;
import java.util.Properties;

public class BeanFactory {
	Properties props = new Properties();
	
	public BeanFactory(InputStream ips){
		try {
			props.load(ips);
        } catch (Exception e) {
        	e.printStackTrace();
        }
		
	}
	
	public Object getBean(String name){
		String clazzName = props.getProperty(name);
		Object bean = null;
		try {
			Class clazz = Class.forName(clazzName);
			// javabean必须要不带参数的构造方法
			bean = clazz.newInstance();
			if(bean instanceof ProxyFactoryBean){
				Object proxy = null;
				ProxyFactoryBean proxyFactoryBean = (ProxyFactoryBean)bean;
				try {
					Advice advice = (Advice)Class.forName(props.getProperty(name + ".advice")).newInstance();
					Object target = Class.forName(props.getProperty(name + ".target")).newInstance();
					proxyFactoryBean.setAdvice(advice);
					proxyFactoryBean.setTarget(target);
					proxy = proxyFactoryBean.getProxy();
                } catch (Exception e) {
                	e.printStackTrace();
                }
				return proxy;
			}
        } catch (Exception e) {
	        e.printStackTrace();
        }
		return bean;
	}
}
